import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/11/5
 * Time: 9:05
 * Description:
 */
class TreeNode {
    public char val;
    public TreeNode left;//null
    public TreeNode right;//null

    public TreeNode(char val) {
        this.val = val;
    }
}
public class TestBinaryTree {

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    // 前序遍历
    void preOrderTraversal(TreeNode root) {
        if(root == null) return;
        System.out.print(root.val+" ");
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);
    }

    // 中序遍历
    void inOrderTraversal(TreeNode root) {
        if(root == null) return;
        inOrderTraversal(root.left);
        System.out.print(root.val+" ");
        inOrderTraversal(root.right);
    }

    // 后序遍历
    void postOrderTraversal(TreeNode root) {
        if(root == null) return;
        postOrderTraversal(root.left);
        postOrderTraversal(root.right);
        System.out.print(root.val+" ");
    }

    /**
     * 前序遍历，把遍历结果存储在，List当中。
     * 子问题：
     * @param root
     * @return
     */
    public List<Character> preorderTraversal2(TreeNode root) {
        List<Character> list = new ArrayList<>();
        if(root == null) return null;
        list.add(root.val);

        List<Character> leftTree = preorderTraversal2(root.left);
        list.addAll(leftTree);

        List<Character> rightTree =preorderTraversal2(root.right);
        list.addAll(rightTree);

        return list;
    }

  /*
  遍历思路：每次遍历到了，一个需要的值，就放到了list里面
   List<Integer> list = new ArrayList<>();

    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null) return list;
        System.out.print(root.val+" ");
        list.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return list;
    }*/

    // 遍历思路-求结点个数
    static int size = 0;
    void getSize1(TreeNode root) {
        if(root == null) {
            return;
        }
        size++;
        getSize1(root.left);
        getSize1(root.right);
    }

    // 子问题思路-求结点个数
    int getSize2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return getSize2(root.left) + getSize2(root.right)+1;
    }

    // 遍历思路-求叶子结点个数
    static int leafSize = 0;
    void getLeafSize1(TreeNode root){
        if(root == null) return;
        if(root.left == null && root.right == null) {
            leafSize++;
        }else {
            getLeafSize1(root.left);
            getLeafSize1(root.right);
        }
    }

    // 子问题思路-求叶子结点个数
    int getLeafSize2(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null && root.right == null) {
            return 1;
        }else {
            return getLeafSize2(root.left)+getLeafSize2(root.right);
        }
    }

    // 子问题思路-求第 k 层结点个数
    int getKLevelSize(TreeNode root,int k) {
        if(root == null) return 0;
        if(k == 1) {
            return 1;
        }
        return getKLevelSize(root.left,k-1)
                +getKLevelSize(root.right,k-1);
    }

    // 获取二叉树的高度
    int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);
        return (leftHeight > rightHeight) ? leftHeight+1 : rightHeight+1;
    }
    // 查找 val 所在结点，没有找到返回 null
    // 按照 根 -> 左子树 -> 右子树的顺序进行查找
    // 一旦找到，立即返回，不需要继续在其他位置查找
    TreeNode find(TreeNode root, char val) {
        if(root == null) return null;
        if(root.val == val)  return root;

        TreeNode ret = find(root.left,val);
        if(ret != null) return ret;

        ret = find(root.right,val);
        if(ret != null) return ret;
        return null;
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q != null || p != null && q == null) {
            return false;
        }
        //有可能2个都为空 或者  都不为空
        if(p == null && q == null) {
            return true;
        }
        //都不为空
        if(p.val != q.val) {
            return false;
        }
        //都不为空 且val值是一样的
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }


    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;

        int leftH = maxDepth(root.left);

        int rihgtH = maxDepth(root.right);


        return Math.abs(leftH-rihgtH) <= 1
                && isBalanced(root.left) && isBalanced(root.right);

    }

    public int isBalancedChild(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = isBalancedChild(root.left);
        int rightHeight = isBalancedChild(root.right);

        if(leftHeight>=0 && rightHeight>=0
                && Math.abs(leftHeight-rightHeight) <= 1) {
            return Math.max(leftHeight,rightHeight)+1;
        }else{
            return -1;
        }
    }


    public boolean isBalanced2(TreeNode root) {
        if(root == null) return true;
        return isBalancedChild(root) >= 0;
    }

    public boolean isSymmetricChild(TreeNode p,TreeNode q) {
        if(p == null && q != null || p != null && q == null) {
            return false;
        }
        if(p == null && q == null) {
            return true;
        }
        if(p.val != q.val) {
            return false;
        }
        return isSymmetricChild(p.left,q.right) && isSymmetricChild(p.right,q.left);
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSymmetricChild(root.left,root.right);
    }


    // 层序遍历
    void levelOrderTraversal(TreeNode root) {
        if(root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");

            if(cur.left!=null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    public List<List<Character>> levelOrder(TreeNode root) {
        List<List<Character>> ret = new ArrayList<>();
        if(root == null) return ret;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();//2
            List<Character> list = new ArrayList<>();//每一层
            while (size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                size--;//1 0
                if(cur.left!=null) {
                    queue.offer(cur.left);
                }
                if(cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(list);
        }
        return ret;
    }

    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if(root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if(cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode top = queue.peek();
            if(top != null) {
                return false;
            }
            queue.poll();
        }
        return true;
    }

}
